﻿using System;
using System.Net;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.ComponentModel.Composition;

using Seesmic.Sdp.Extensibility;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using PTiRouZ.Net.SilverLight.Api.HelloTipi;
using Seesmic.Sdp.Utils;



namespace Seesmic.HelloTipi
{
    [Export(typeof(IPlugin))]
    public class HelloTipiPlugin : IPlugin
    {
        //private static readonly ResourceDictionary _Resources;
        #region Constructors
        private static readonly ResourceDictionary TemplateResources;

        public const string PluginName = "HelloTipiPlugin";

        public static readonly Guid PluginId = new Guid("AE8B66F6-37BE-4655-BF9C-C1B8CB4D61CE");

        //private static ObservableCollection<string> _sitesNameList;
        public static ObservableCollection<string> SitesNameList
        {
            get
            {
                /*_sitesNameList = StorageService.GetValue<ObservableCollection<string>>(PluginId, "SitesNameList");
                if (_sitesNameList == null)
                    _sitesNameList = new ObservableCollection<string>();
                
                return _sitesNameList;*/

                if (StorageService.GetValue<ObservableCollection<string>>(PluginId, "SitesNameList") == null)
                    return new ObservableCollection<string>();
                else
                    return StorageService.GetValue<ObservableCollection<string>>(PluginId, "SitesNameList");
            }
            set
            {
                //_sitesNameList = value;
                StorageService.SetValue<ObservableCollection<string>>(PluginId, "SitesNameList", value);
            }
           
        }

        //private static ObservableCollection<my_sites> _sitesList;
        public static ObservableCollection<my_sites> SitesList
        {
            get
            {
                /*_sitesList = StorageService.GetValue<ObservableCollection<my_sites>>(PluginId, "SitesList");
                if (_sitesList == null)
                    _sitesList = new ObservableCollection<my_sites>();                                   

                return _sitesList;*/

                if (StorageService.GetValue<ObservableCollection<my_sites>>(PluginId, "SitesList") == null)
                    return new ObservableCollection<my_sites>();
                else
                    return StorageService.GetValue<ObservableCollection<my_sites>>(PluginId, "SitesList");
            }
            set
            {
                //_sitesList = value;
                StorageService.SetValue<ObservableCollection<my_sites>>(PluginId, "SitesList", value);
            }
        }

        public HelloTipiPlugin() {            
        }

        static HelloTipiPlugin()
        {
            ResourceDictionary templates = new ResourceDictionary();
            templates.Source = new Uri("/HelloTipiPlugin;component/Templates/DataTemplates.xaml", UriKind.Relative);
            TemplateResources = templates;
        }

        #endregion

        #region IPlugin Members
        public Guid Id
        {
            get
            {
                return PluginId;
            }
        }


        public DataTemplate SettingsTemplate
        {
            get
            {
                return null;
            }
        }

        public void Initialize()
        {
            //LogInfo("public void Initialize()");
            //TODO - fill in initialization code here
            //ITimelineItemAction addToGroup = new AddToGroup();
            //ShellService.RegisterTimelineItemAction(addToGroup);
        }

        public void CommitSettings()
        {
            //LogInfo("public void CommitSettings()");
            //TODO - if your plugin provides a settings UI, here you can persist your settings
        }

        public void RevertSettings()
        {
            //LogInfo("public void RevertSettings()");
            //TODO - if your plugin provides a settings UI, here you can revert your settings

        }

        #endregion

        #region Templates
        /*internal static DataTemplate SmallLogoTemplate
        {
            get
            {
                return (DataTemplate)TemplateResources["SmallLogoTemplate"];
            }
        }*/

        public static DataTemplate AccountIcon
        {
            get
            {
                return (DataTemplate)TemplateResources["HelloTipiAccountIcon"];
            }
        }

        public static DataTemplate SmallIcon
        {
            get
            {
                return (DataTemplate)TemplateResources["HelloTipiSmallIcon"];
            }
        }


        public static DataTemplate EditAccountTemplate
        {
            get
            {
                return (DataTemplate)TemplateResources["EditAccountTemplate"];
            }
        }

        public static DataTemplate NewAccountTemplate
        {
            get
            {
                return (DataTemplate)TemplateResources["NewAccountTemplate"];
            }
        }

        public static DataTemplate TimelineItemTemplate
        {
            get
            {
                return (DataTemplate)TemplateResources["TimelineItemTemplate"];
            }
        }

        public static DataTemplate NotificationItemTemplate
        {
            get
            {
                return (DataTemplate)TemplateResources["NotificationItemTemplate"];
            }
        }

        public static DataTemplate NotificationToastHeader
        {
            get
            {
                return (DataTemplate)TemplateResources["NotificationToastHeader"];
            }
        }
        
        public static DataTemplate PhotosTemplate
        {
            get
            {
                return (DataTemplate)TemplateResources["PhotosTemplate"];
            }
        }

        public static DataTemplate PhotosHeaderTemplate
        {
            get
            {
                return (DataTemplate)TemplateResources["PhotosHeaderTemplate"];
            }
        }

        

        internal static DataTemplate MentionActionIcon
        {
            get
            {
                return (DataTemplate)TemplateResources["MentionActionIcon"];
            }
        }

        internal static DataTemplate PrivateActionIcon
        {
            get
            {
                return (DataTemplate)TemplateResources["PrivateActionIcon"];
            }
        }


        public static DataTemplate OneAccountOnly
        {
            get
            {
                return (DataTemplate)TemplateResources["OneAccountOnly"];
            }
        }

        internal static DataTemplate HelloTipiProfileTemplate
        {
            get
            {
                return (DataTemplate)TemplateResources["HelloTipiProfileTemplate"];
            }
        }


        #endregion

        #region Shell Imports
        private static IShellService _shell;
        private static INotificationService _notify;
        private static ILogService _log;
        private static IStorageService _storage;
        private static IPostingService _PostingService;
        private static ISessionService _SessionService;

        [Import]
        public IShellService ShellServiceServiceImport { set { _shell = value; } }

        [Import]
        public INotificationService NotificationServiceImport { set { _notify = value; } }

        [Import]
        public ILogService LogServiceImport { set { _log = value; } }

        [Import]
        public IStorageService StorageServiceImport { set { _storage = value; } }

        [Import]
        public IPostingService PostingServiceImport { set { _PostingService = value; } }

        [Import]
        public ISessionService SessionServiceImport { set { _SessionService = value; } }

        public static IShellService ShellService { get { return _shell; } }
        public static ILogService LogService { get { return _log; } }
        public static INotificationService NotifyService { get { return _notify; } }
        public static IStorageService StorageService { get { return _storage; } }
        public static IPostingService PostingService { get { return _PostingService; } }
        public static ISessionService SessionService { get { return _SessionService; } }

        #endregion


        internal static void LogInfo(string message)
        {
            LogService.Info(string.Format("Plug-in: HelloTipi: {0}", message));
        }

        internal static string getUserNameOnly(string UserName)
        {
            if (UserName.Contains("#"))
            {
                return UserName.Substring(0, UserName.IndexOf("#"));
            }
            else
                return UserName;
        }

        internal static string getFamilyNameOnly(string UserName)
        {
            if (UserName.Contains("#"))
            {
                return UserName.Substring(UserName.IndexOf("#") + 1);
            }
            else
                return UserName;
        }

        internal static my_sites getSite(string UserOrFamilyName)
        {
            return (from my_sites s in SitesList
                    where s.name.Equals(getFamilyNameOnly(UserOrFamilyName))
                    select s).FirstOrDefault();
        }

        #region Notifications
        internal static void ShowBarNotification(string text, string messageFormat, params object[] formatParams)
        {
            string str = string.Format(messageFormat, formatParams);
            BarNotificationInfo info2 = new BarNotificationInfo();
            info2.Text = text;
            if (!String.IsNullOrEmpty(str))
                info2.Text += " : " + str;
            BarNotificationInfo notificationInfo = info2;
            NotifyService.ShowBarNotification(notificationInfo);
        }

        internal static void ShowBarNotification(string text, string messageFormat, BarNotificationKind barKind, TimeSpan? duration, params object[] formatParams)
        {
            string str = string.Format(messageFormat, formatParams);
            BarNotificationInfo info2 = new BarNotificationInfo();
            info2.Text = text;
            if (!String.IsNullOrEmpty(str))
                info2.Text += " : " + str;
            info2.Kind = barKind;
            BarNotificationInfo notificationInfo = info2;
            NotifyService.ShowBarNotification(notificationInfo);
        }

        internal static void ShowDialogNotification(string title, string messageFormat, params object[] formatParams)
        {
            string str = string.Format(messageFormat, formatParams);
            DialogNotificationInfo info2 = new DialogNotificationInfo();
            info2.Title = title;
            info2.Text = str;
            info2.Buttons = DialogNotificationButtons.OK;
            DialogNotificationInfo notificationInfo = info2;
            NotifyService.ShowDialogNotification(notificationInfo);
        }

        internal static void ShowDialogNotification(string title, string messageFormat, DialogNotificationButtons buttons, params object[] formatParams)
        {
            string str = string.Format(messageFormat, formatParams);
            DialogNotificationInfo info2 = new DialogNotificationInfo();
            info2.Title = title;
            info2.Text = str;
            info2.Buttons = buttons;
            DialogNotificationInfo notificationInfo = info2;
            NotifyService.ShowDialogNotification(notificationInfo);
        }

        
        /*var notificationInfo = new ToastNotificationInfo()
    {
         Icon = accountIcon,
         Content = content,
         Header = header,
         HeaderTemplate = headerTemplate
    };

    SynchronizationHelper.Post( (actionParam) =>
    {

NotificationService.ShowToastNotification((ToastNotificationInfo)actionParam);
    }, notificationInfo);
        */
        internal static void ShowToastNotification(object header, object content)
        {
            var notificationInfo = new ToastNotificationInfo()
            {
                Icon = DataTemplateHelper.DataTemplateFromImage("/HelloTipiPlugin;component/Resources/HelloTipi.png"),
                Content = content,
                Header = header,
                HeaderTemplate = NotificationToastHeader,
                ContentTemplate = NotificationItemTemplate
            };

            SynchronizationHelper.Post((actionParam) =>
            {
                NotifyService.ShowToastNotification((ToastNotificationInfo)actionParam);
            }, notificationInfo);
            
            /*ToastNotificationInfo info2 = new ToastNotificationInfo();
            info2.Icon = 
            info2.Content = content;
            info2.Header = header;
            ToastNotificationInfo notificationInfo = info2;
            NotifyService.ShowToastNotification(notificationInfo);
            return notificationInfo;*/
        }

        internal static void ShowToastNotification(object header, string contentText)
        {
            ShowToastNotification(header, contentText);
        }
        #endregion
    }
}